package com.utc.cards.table.jade.agent.hostAgent;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PlayerActivityListenerBehaviour extends CyclicBehaviour
{
    private static Logger log = LoggerFactory.getLogger(PlayerActivityListenerBehaviour.class);

    private static final long serialVersionUID = -2011056274256357023L;
    private HostAgent agent;

    private List<String> users_ready;

    public PlayerActivityListenerBehaviour(HostAgent hostAgent)
    {
        this.agent = hostAgent;
        users_ready = hostAgent.getModel().getReadyList();
    }

    @Override
    public void action()
    {
        ACLMessage message = agent.receive(MessageTemplate.MatchConversationId("active"));
        if (message != null)
        {
            log.debug("PlayerActivityListenerBehaviour : message !");
            String senderName = message.getSender().getLocalName();
            log.debug("sender.localName : {}", senderName);
            if (!users_ready.contains(senderName))
            {
                log.debug("adding to ready users : {}", senderName);
                users_ready.add(senderName);
            }
            log.debug("PlayerActivityListenerBehaviour ({}/{})", users_ready.size(), agent.getModel().getGame().getMinPlayerCount());
            if (users_ready.size() == agent.getModel().getGame().getMinPlayerCount())
            {
                log.debug("PlayerActivityListenerBehaviour OK");
            }
        }
        block();
    }

}
